﻿using NetCore8Demo.Models.Axis;
using NetCore8Demo.Models.Param;
using NetCore8Demo.Models.PLC;
using NetCore8Demo.Views.Param;
using System.Xml.Linq;

namespace NetCore8Demo.Views.Axis
{
    public partial class AxisSetForm : Form
    {
        public AxisSetForm()
        {
            InitializeComponent();
        }
        private  List<AxisInfo> _axisInfoList = new();

        public void InitControl()
        {

        }
        #region 变量定义
        short selAxis = 0;
        string selAxisName = "";
        int _comIndex;
        #endregion

        #region 初始化

        //窗体加载
        private void AxisForm_Load(object sender, EventArgs e)
        {
            LoadAxis();
           
        }

        //窗体关闭
        private void AxisForm_FormClosing(object sender, FormClosingEventArgs e)
        {
         
        }




        #endregion

        #region 通用设置,跟运动无关的
        //新增轴
        private void NewAxisBtn_Click(object sender, EventArgs e)
        {
            List<ParamInfo> paramList = [];
            int tempIndex = 0;
            if (_axisInfoList.Count > 0)
            {
                tempIndex = _axisInfoList.Max(t => t.Index) + 1;
            }
            paramList.Add(new ParamInfo("序号", tempIndex + ""));
            paramList.Add(new ParamInfo("轴号"));
            paramList.Add(new ParamInfo("名称"));

            BasicParamForm basicParamForm = new BasicParamForm(paramList);
            basicParamForm.ShowDialog();

            if (!basicParamForm.SureFlag)
            {
                return;
            }

            short.TryParse(paramList[1].NewValue, out short axisId);
            foreach (var item in _axisInfoList)
            {
                if (axisId == item.AxisId)
                {
                    MessageBox.Show("该轴号已存在！");
                    return;
                }
            }
            AxisInfo axisInfo = new AxisInfo();
            int.TryParse(paramList[0].NewValue, out int index);
            axisInfo.Id = Guid.NewGuid();
            axisInfo.Index = index;
            axisInfo.Name = paramList[2].NewValue;

            axisInfo.JogVel = 10;
            axisInfo.JogAcc = 1;
            axisInfo.JogDec = 1;
            axisInfo.JogSmoothTime = 0;

            axisInfo.TrapVel = 10;
            axisInfo.TrapAcc = 1;
            axisInfo.TrapDec = 1;
            axisInfo.TrapSmoothTime = 10;
            

            axisInfo.HomeHighVel = 20;
            axisInfo.HomeLowVel = 20;
            axisInfo.HomeAcc = 5;
            axisInfo.HomeDec = 5;
            axisInfo.HomeModeMode = HomeModeType.限位加原点回零;
            axisInfo.HomeDirection = HomeDirectionType.负方向回零;


            axisInfo.DistanceToPulse = 200;

            _axisInfoList.Add(axisInfo);
            SavaAxis();


        
        }

        //删除轴
        private void DelAxisBtn_Click(object sender, EventArgs e)
        {
            if (AxisListView.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择轴");
                return;
            }
            _comIndex = AxisListView.SelectedIndices[0];
            string id = this.AxisListView.Items[_comIndex].SubItems[0].Text.Trim();
           

            _axisInfoList.RemoveAll(p => p.Id == Guid.Parse(id));

          
            SavaAxis();

        }
        //保存数据并排序加载
        public void SavaAxis()
        {
            //升序排列
            _axisInfoList = _axisInfoList.OrderBy(s => s.Index).ToList();
            StaticAxis.SavaAxisInfoList(_axisInfoList);
            LoadAxis();
        }

        //加载轴
        public void LoadAxis()
        {
            _axisInfoList = StaticAxis.LoadAxisInfoList();
            this.AxisListView.Items.Clear();
            foreach (var axisInfo in _axisInfoList)
            {
                ListViewItem lvi = new ListViewItem(axisInfo.Id + "");
                lvi.SubItems.Add(axisInfo.Index + "");
                lvi.SubItems.Add(axisInfo.AxisId + "");
                lvi.SubItems.Add(axisInfo.Name + "");
                this.AxisListView.Items.Add(lvi);
            }
        }

        //保存轴运动参数
        private void SavaAxisParamBtn_Click(object sender, EventArgs e)
        {
            //if (AxisListView.SelectedItems.Count == 0)
            //{
            //    MessageBox.Show("请先选择轴");
            //    return;
            //}
            //_comIndex = AxisListView.SelectedIndices[0];
            //int axisId = Convert.ToInt32(this.AxisListView.Items[_comIndex].SubItems[1].Text.Trim());
            //for (int i = 0; i < Global.axisInfoList.Count; i++)
            //{
            //    if (axisId == Global.axisInfoList[i].ID)
            //    {
            //        Global.axisInfoList[i].jogVel = Convert.ToDouble(this.jogVelTB.Text);
            //        Global.axisInfoList[i].jogAcc = Convert.ToDouble(this.jogAccTB.Text);
            //        Global.axisInfoList[i].jogDec = Convert.ToDouble(this.jogDecTB.Text);
            //        Global.axisInfoList[i].jogSmoothtime = Convert.ToDouble(this.jogSmoothTimeTB.Text);

            //        Global.axisInfoList[i].trapVel = Convert.ToDouble(this.trapVelTB.Text);
            //        Global.axisInfoList[i].trapAcc = Convert.ToDouble(this.trapAccTB.Text);
            //        Global.axisInfoList[i].trapDec = Convert.ToDouble(this.trapDecTB.Text);
            //        Global.axisInfoList[i].trapSmoothtime = (short)Convert.ToInt32(this.trapSmoothTimeTB.Text);

            //        Global.axisInfoList[i].homeHighVel = (short)Convert.ToInt32(this.homeHighVelTB.Text);
            //        Global.axisInfoList[i].homeLowVel = (short)Convert.ToInt32(this.homeLowVelTB.Text);

            //        Global.axisInfoList[i].homeAcc = Convert.ToDouble(this.homeAccTB.Text);
            //        Global.axisInfoList[i].homeDec = Convert.ToDouble(this.homeDecTB.Text);

            //        Global.axisInfoList[i].pulseToDistance = Convert.ToDouble(this.pulseToDistanceTB.Text);

            //        switch (this.homeModeCB.SelectedIndex)
            //        {
            //            case 0:
            //                Global.axisInfoList[i].homeMode = 10;
            //                break;
            //            case 1:
            //                Global.axisInfoList[i].homeMode = 20;
            //                break;
            //            case 2:
            //                Global.axisInfoList[i].homeMode = 11;
            //                break;
            //            default: break;
            //        }

            //        if (this.homeDirectionCB.SelectedIndex == 0)
            //        {
            //            Global.axisInfoList[i].homeDirection = 1;
            //        }
            //        else
            //        {
            //            Global.axisInfoList[i].homeDirection = -1;
            //        }
            //        break;
            //    }
            //}
            //SavaAxis();
        }

        //轴的单击事件
        private void AxisListView_Click(object sender, EventArgs e)
        {
            _comIndex = AxisListView.SelectedIndices[0];
            string axisId = this.AxisListView.Items[_comIndex].SubItems[1].Text.Trim();
            selAxisName = this.AxisListView.Items[_comIndex].SubItems[2].Text.Trim();
            AxisInfo axisObject = GetAxisDetailById(Convert.ToInt32(axisId));
            selAxis = Convert.ToInt16(axisId);
            showAxisControl(axisObject);
        }

        //双击轴
        private void AxisListView_DoubleClick(object sender, EventArgs e)
        {
            //_comIndex = AxisListView.SelectedIndices[0];
            //string oldIndex = this.AxisListView.Items[_comIndex].SubItems[0].Text.Trim();
            //string oldAxisId = this.AxisListView.Items[_comIndex].SubItems[1].Text.Trim();
            //string oldAxisName = this.AxisListView.Items[_comIndex].SubItems[2].Text.Trim();
            //ThreePram threeParm = new ThreePram("排序号", "轴号", "名称");
            //threeParm.setValue(oldIndex, oldAxisId, oldAxisName);
            //threeParm.ShowDialog();
            //if (threeParm.Tag.ToString() != null && threeParm.Tag.ToString() != "")
            //{
            //    string[] strArr = threeParm.Tag.ToString().Split(',');
            //    int newIndex = Convert.ToInt32(strArr[0]);
            //    short newAxisID = Convert.ToInt16(strArr[1]);
            //    string newName = strArr[2];

            //    for (int i = 0; i < Global.axisInfoList.Count; i++)
            //    {
            //        //没修改直接不用验证了
            //        if (newAxisID == Convert.ToInt16(oldAxisId))
            //        {
            //            break;
            //        }

            //        if (newAxisID == Global.axisInfoList[i].ID)
            //        {
            //            MessageBox.Show("该点位已存在！");
            //            return;
            //        }
            //    }

            //    for (int i = 0; i < Global.axisInfoList.Count; i++)
            //    {
            //        if (Convert.ToInt16(oldAxisId) == Global.axisInfoList[i].ID)
            //        {
            //            Global.axisInfoList[i].index = newIndex;
            //            Global.axisInfoList[i].ID = newAxisID;
            //            Global.axisInfoList[i].name = newName;
            //            break;
            //        }
            //    }
            //}
            //SavaAxis();
        }

        //获取轴的详情
        public AxisInfo GetAxisDetailById(int id)
        {
            AxisInfo axisResult = new AxisInfo();
            //foreach (AxisInfo axis in Global.axisInfoList)
            //{
            //    if (axis.ID == id)
            //    {
            //        axisResult = axis;
            //        break;
            //    }
            //}
            return axisResult;
        }
        //展示轴参数
        public void showAxisControl(AxisInfo axis)
        {
            //this.jogVelTB.Text = axis.jogVel.ToString();
            //this.jogAccTB.Text = axis.jogAcc.ToString();
            //this.jogDecTB.Text = axis.jogDec.ToString();
            //this.jogSmoothTimeTB.Text = axis.jogSmoothtime.ToString();

            //this.trapVelTB.Text = axis.trapVel.ToString();
            //this.trapAccTB.Text = axis.trapAcc.ToString();
            //this.trapDecTB.Text = axis.trapDec.ToString();
            //this.trapSmoothTimeTB.Text = axis.trapSmoothtime.ToString();

            //this.homeHighVelTB.Text = axis.homeHighVel.ToString();
            //this.homeLowVelTB.Text = axis.homeLowVel.ToString();
            //this.homeAccTB.Text = axis.homeAcc.ToString();
            //this.homeDecTB.Text = axis.homeDec.ToString();

            //this.pulseToDistanceTB.Text = axis.pulseToDistance.ToString();
            //switch (axis.homeMode)
            //{
            //    case 10:
            //        this.homeModeCB.SelectedIndex = 0;
            //        break;
            //    case 20:
            //        this.homeModeCB.SelectedIndex = 1;
            //        break;
            //    case 11:
            //        this.homeModeCB.SelectedIndex = 2;
            //        break;
            //    default: break;
            //}

            //if (axis.homeDirection==1)
            //{
            //    this.homeDirectionCB.SelectedIndex = 0;
            //}
            //else
            //{
            //    this.homeDirectionCB.SelectedIndex = 1;
            //}
        }
        #endregion

       

       
        

       

      
    }
}
